$(document).ready(function(){
	//列表下拉
	$('img[nc_type="flex"]').click(function(){
		var status = $(this).attr('status');
		if(status == 'open'){
			var pr = $(this).parent('td').parent('tr');
			var id = $(this).attr('fieldid');
			var obj = $(this);
			$(this).attr('status','none');
			$.ajax({
				url: '/admin/goods_category/index/ajax/1/parent_id/'+id,
				dataType: 'json',
				success: function(data){
					var src='';
					for(var i = 0; i < data.length; i++){
						var tmp_vertline = "<img class='preimg' src='__IMG__/images/vertline.gif'/>";
						src += "<tr class='" + pr.attr('class') + " row" + id + "'>";
						src += "<td class='w36'><input type='checkbox' name='id[]' value='" + data[i].category_id + "' class='checkitem'>";
						//图片
						src += " <img fieldid='"+data[i].category_id+"' status='none' nc_type='flex' src='" + SITEIMG + "/tv-item.gif' />";
						src += "</td><td class='w48 sort'>";						
						//排序
						src += " <span title='可编辑下级分类排序' ajax_branch='category_sort' datatype='number' fieldid='" + data[i].category_id + "' fieldname='sort' nc_type='inline_edit' class='editable tooltip'>"+data[i].sort+"</span></td>";
						//名称
						src += "<td class='name'>";
						src += " <img fieldid='"+data[i].category_id+"' status='none' nc_type='flex' src='" + SITEIMG + "/tv-expandable1.gif' />";
						src += " <span title='可编辑' required='1' fieldid='" + data[i].category_id + "' ajax_branch='category_name' fieldname='category_name' nc_type='inline_edit' class='editable tooltip'>" + data[i].category_name + "</span>";
						src += "</td>";
						//操作
						src += "<td class='align-center'>";
						src += "<a href='/admin/goods_category/edit/id/" + data[i].category_id + "'>编辑</a>";
						src += " | <a href=\"javascript:if(confirm('删除该分类将会同时删除该分类的所有下级分类，您确定要删除吗?'))window.location = '/admin/goods_category/delete/id/" + data[i].category_id + "';\">删除</a>";
						src += "</td>";
						src += "</tr>";
					}
					//插入
					pr.after(src);
					obj.attr('status','close');
					obj.attr('src',obj.attr('src').replace("tv-expandable","tv-collapsable"));
					$('img[nc_type="flex"]').unbind('click');
					$('span[nc_type="inline_edit"]').unbind('click');
					//重现初始化页面
                    $.getScript(SITEJS + "/jquery.edit.js");
					$.getScript(SITEJS + "/jquery.goods_category.js");
					$.getScript(SITEJS + "/admincp.js");
				},
				error: function(){
					alert('获取信息失败');
				}
			});
		}
		if(status == 'close'){
			$(".row"+$(this).attr('fieldid')).remove();
			$(this).attr('src',$(this).attr('src').replace("tv-collapsable","tv-expandable"));
			$(this).attr('status','open');
		}
	})
});